package com.xuecheng.checkcode.service.impl;

import com.xuecheng.checkcode.service.SendCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 发送验证码服务
 */
@Slf4j
@Service
public class SendCodeServiceImpl implements SendCodeService {
    @Resource
    JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    String fromEmail;       //发送方邮箱

    /**
     * 向指定邮箱发送验证码
     * @param toEmail
     * @param code
     * @return
     */
    @Override
    public boolean sendEmailCode(String toEmail, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(toEmail);
        message.setSubject("学成在线-验证码通知-独立");
        message.setText("你的验证码为：" + code + "，5分钟内有效");
        mailSender.send(message);
        return true;
    }
}
